﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Job.Core.Data;

namespace Job.Core
{
    /// <summary>
    /// 通用查询规则
    /// </summary>
    [Serializable]
    public class SearchQuery
    {
        #region Properties

        private int _pageIndex = 1;
        private int _pageSize = 10;
        private int _totalRecords = 0;
        private bool blOrderType = false;
        private string _strOrder = string.Empty;
        private string _strFilter = string.Empty;
        private string _strGroup = string.Empty;
        private string _strColumn = string.Empty;
        private string _strTableName = string.Empty;
        private string _strQuery = string.Empty;
        private Dictionary<SqlLogic, QuerySingle> _queryList = new Dictionary<SqlLogic, QuerySingle>();
        #endregion


        #region
        /// <summary>
        /// 分页排序字段 排序规则
        /// </summary>
        public bool OrderType
        {
            get { return blOrderType; }
            set { this.blOrderType = value; }
        }
        /// <summary>
        /// 表名字
        /// </summary>
        public string StrTableName
        {
            get { return _strTableName; }
            set { this._strTableName = value; }
        }
        /// <summary>
        /// 不带where
        /// </summary>
        public string StrFilter
        {
            get { return _strFilter; }
            set { _strFilter = value; }
        }
        /// <summary>
        /// 查询规则集合 使用之后 StrFilter 无效
        /// </summary>
        public Dictionary<SqlLogic, QuerySingle> QueryList
        {
            get { return _queryList; }
            set { _queryList = value; }
        }
        /// <summary>
        /// 不带Order by
        /// </summary>
        public string StrOrder
        {
            get { return string.IsNullOrEmpty(_strOrder) ? "ID" : _strOrder; }
            set { _strOrder = value; }
        }
        /// <summary>
        /// 不带Group by
        /// </summary>
        public string StrGroup
        {
            get { return _strGroup; }
            set { _strGroup = value; }
        }
        /// <summary>
        /// *
        /// </summary>
        public string StrColumn
        {
            get { return string.IsNullOrEmpty(_strColumn) ? "*" : string.Format("{0}", _strColumn); }
            set { _strColumn = value; }
        }

        /// <summary>
        /// 每页显示记录数
        /// </summary>
        public int PageSize
        {
            get { return _pageSize < 1 ? 10 : _pageSize; }
            set { _pageSize = value; }
        }

        /// <summary>
        /// 当前显示页面页码
        /// </summary>
        public int PageIndex
        {
            get { return _pageIndex < 1 ? 1 : _pageIndex; }
            set { _pageIndex = value; }
        }

        /// <summary>
        /// 总记录
        /// </summary>
        public int TotalRecords
        {
            get { return _totalRecords; }
            set { _totalRecords = value; }
        }
        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPage
        {
            get { return ((TotalRecords % PageSize == 0) ? TotalRecords / PageSize : TotalRecords / PageSize); }
        }
        /// <summary>
        /// 完整的查询语句
        /// </summary>
        public string Query
        {
            get { return _strQuery; }
            set { _strQuery = value; }
        }
        /// <summary>
        /// 构造查询语句
        /// </summary>
        public string GetQuery
        {
            get
            {
                if (!string.IsNullOrEmpty(Query))
                    return Query;

                if (QueryList.Count != 0)
                {
                    StringBuilder sb = new StringBuilder();
                    bool isFrist = true;
                    foreach (KeyValuePair<SqlLogic, QuerySingle> obj in _queryList)
                    {
                        if (isFrist)
                        {
                            isFrist = false;
                            sb.AppendFormat(" {1} ", obj.Value.GetThisQuery);

                        }
                        else
                        {
                            sb.AppendFormat(" {0} {1} ", obj.Key, obj.Value.GetThisQuery);
                        }
                    }
                    this.StrFilter = sb.ToString();
                }

                Query = SYSequenceDAL.BuildSql(this.StrFilter, this.PageSize, this.PageIndex, this.OrderType, this.StrColumn, this.StrOrder, this.StrTableName);

                return Query;
            }
        }

        #endregion
    }
    

}